import pygame as pg
from Settings import *
from pygame.math import Vector2 as vec

from Map_Generater import *
from math import cos

wall_sprite = pg.sprite.Group()
'''class Wall(pg.sprite.Sprite):
    def __init__(self,index,scene):
        pg.sprite.Sprite.__init__(self)
        #映射坐标乘以一个格子的宽度和高度 生成墙壁坐标
        self.rect = pg.Rect((boundary[index][0])*SceneSettings.tileWidth, (boundary[index][1])*SceneSettings.tileHeight, SceneSettings.tileWidth, SceneSettings.tileHeight)
        self.posy=self.rect.y
        self.scene=scene
    def update(self,dt):
        self.rect.y=-self.scene.cameraY+self.posy'''
class Weapon(pygame.sprite.Sprite):
    def __init__(self,player):
        pygame.sprite.Sprite.__init__(self)
        self.direction = player.status
        # graphic
        self.image=pygame.image.load(f"assets/sword/{self.direction}.png").convert_alpha()
        self.update(player)
    def update(self,player):
        if self.direction == 'right':
            self.image=pygame.transform.scale(self.image,(40,20))
            self.rect = self.image.get_rect(midleft = player.rect.midright + pygame.math.Vector2(0,16))
        elif self.direction == 'left': 
            self.image=pygame.transform.scale(self.image,(40,20))
            self.rect = self.image.get_rect(midright = player.rect.midleft + pygame.math.Vector2(0,16))
        elif self.direction == 'down':
            self.image=pygame.transform.scale(self.image,(20,40))
            self.rect = self.image.get_rect(midtop = player.rect.midbottom + pygame.math.Vector2(-10,0))
        else:
            self.image=pygame.transform.scale(self.image,(20,40))
            self.rect = self.image.get_rect(midbottom = player.rect.midtop + pygame.math.Vector2(-10,0))
class Player(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.load_images()
        self.load_muisic()
        self.sword=None
        self.sword_timer=0
        self.image = self.walking_frames_down[0]
        self.rect = self.image.get_rect()
        self.rect.center = (WindowSettings.width / 2, WindowSettings.height / 2)
        self.pos = vec(WindowSettings.width / 2, WindowSettings.height / 2)
        self.direction = vec(0, 0)#方向向量
        self.frame_index = 0#帧索引
        self.last_update = 0#上次更新时间
        self.last_update_attack = 0
        self.status = 'down'
        self.current_frames = self.walking_frames_down
        self.attacking = False
        self.attack_direction = vec(0, 0)
        self.speed = PlayerSettings.speed
        self.if_change_camera = False
        self.health=PlayerSettings.health
        self.attack_damage=PlayerSettings.attack_damage
        self.gold=0
        self.vulnerable = True
        self.hurt_time = 0
    def load_muisic(self):
        self.attack_sound = pg.mixer.Sound(r".\assets\audio\slash.wav")
        self.attack_sound.set_volume(10)
    def load_images(self):
        self.walking_frames_up = []
        self.walking_frames_down = []
        self.walking_frames_left = []
        self.walking_frames_right = []
        self.attack_frames_up = []
        self.attack_frames_down = []
        self.attack_frames_left = []
        self.attack_frames_right = []
        self.idle_frames_up = []
        self.idle_frames_down = []
        self.idle_frames_left = []
        self.idle_frames_right = []
        ''' # 步行动画导入
        for i in range(0, 4):  
            self.walking_frames_up.append(pg.image.load(f'assets/sprites/player/up/up_{i}.png').convert_alpha())
            self.walking_frames_down.append(pg.image.load(f'assets/sprites/player/down/down_{i}.png').convert_alpha())
            self.walking_frames_left.append(pg.image.load(f'assets/sprites/player/left/left_{i}.png').convert_alpha())
            self.walking_frames_right.append(pg.image.load(f'assets/sprites/player/right/right_{i}.png').convert_alpha())
        # 攻击动画导入
        self.attack_frames_up.append(pg.image.load(f'assets/sprites/player/up_attack/attack_up.png').convert_alpha())
        self.attack_frames_down.append(pg.image.load(f'assets/sprites/player/down_attack/attack_down.png').convert_alpha())
        self.attack_frames_left.append(pg.image.load(f'assets/sprites/player/left_attack/attack_left.png').convert_alpha())
        self.attack_frames_right.append(pg.image.load(f'assets/sprites/player/right_attack/attack_right.png').convert_alpha())
        # 站立动画导入
        self.idle_frames_up.append(pg.image.load(f'assets/sprites/player/up_idle/idle_up.png').convert_alpha())
        self.idle_frames_down.append(pg.image.load(f'assets/sprites/player/down_idle/idle_down.png').convert_alpha())
        self.idle_frames_left.append(pg.image.load(f'assets/sprites/player/left_idle/idle_left.png').convert_alpha())
        self.idle_frames_right.append(pg.image.load(f'assets/sprites/player/right_idle/idle_right.png').convert_alpha())'''
        # 步行动画导入
        player_walk_up = [pg.image.load(GamePath.player_walk_up[i]) for i in range(4)]
        self.walking_frames_up = [pg.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha() for image in player_walk_up]
        player_walk_down = [pg.image.load(GamePath.player_walk_down[i]) for i in range(4)]
        self.walking_frames_down = [pg.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha() for image in player_walk_down]
        player_walk_left = [pg.image.load(GamePath.player_walk_left[i]) for i in range(4)]
        self.walking_frames_left = [pg.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha() for image in player_walk_left]
        player_walk_right = [pg.image.load(GamePath.player_walk_right[i]) for i in range(4)]
        self.walking_frames_right = [pg.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha() for image in player_walk_right]
        # 攻击动画导入
        player_attack = [pg.image.load(GamePath.player_attack[i]) for i in range(4)]
        self.attack_frames_up.append(pg.transform.scale(player_attack[0],(SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha()) 
        self.attack_frames_down.append(pg.transform.scale(player_attack[1],(SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha())
        self.attack_frames_left.append(pg.transform.scale(player_attack[2],(SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha()) 
        self.attack_frames_right.append(pg.transform.scale(player_attack[3],(SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha())
        
        player_idle = [pg.image.load(GamePath.player_idle[i]) for i in range(4)]
        self.idle_frames_up.append(pg.transform.scale(player_idle[0], (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha())
        self.idle_frames_down.append(pg.transform.scale(player_idle[1], (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha())
        self.idle_frames_left.append(pg.transform.scale(player_idle[2], (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha())
        self.idle_frames_right.append(pg.transform.scale(player_idle[3], (SceneSettings.tileWidth, SceneSettings.tileHeight)).convert_alpha())


    def input(self):
        keys = pg.key.get_pressed()
        any_key_pressed = False
        if keys[pg.K_LEFT]:
            self.direction.x = -1
            self.status = 'left'
            self.current_frames = self.walking_frames_left
            self.attack_direction = vec(-1, 0)
            any_key_pressed = True
        elif keys[pg.K_RIGHT]:
            self.direction.x = 1
            self.status = 'right'
            self.current_frames = self.walking_frames_right
            self.attack_direction = vec(1, 0)
            any_key_pressed = True
        else:
            self.direction.x = 0
        
        if keys[pg.K_UP]:
            self.status = 'up'
            self.direction.y = -1
            self.current_frames = self.walking_frames_up
            self.attack_direction = vec(0, -1)
            any_key_pressed = True
        elif keys[pg.K_DOWN]:
            self.status = 'down'
            self.direction.y = 1
            self.current_frames = self.walking_frames_down
            self.attack_direction = vec(0, 1)
            any_key_pressed = True
        else:
            self.direction.y = 0
        if keys[pg.K_SPACE]:
            self.attacking = True
            any_key_pressed = True
            self.attack()
        if not any_key_pressed:
            if self.attack_direction.x == 1:
                self.current_frames = self.idle_frames_right
            elif self.attack_direction.x == -1:
                self.current_frames = self.idle_frames_left
            elif self.attack_direction.y == 1:
                self.current_frames = self.idle_frames_down
            elif self.attack_direction.y == -1:
                self.current_frames = self.idle_frames_up
    def move(self, dt):
        old_pos = self.pos
        if self.direction.x != 0 and self.direction.y != 0:
            self.direction *= 0.7071
        self.pos.x += self.direction.x * self.speed * dt
        self.rect.centerx = self.pos.x
        if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
            collide_wall = pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
            if self.direction.x ==1 or self.direction.x == 0.7071:
                 self.pos.x = collide_wall[0].rect.left - self.rect.width / 2
            elif self.direction.x == -1 or self.direction.x == -0.7071:
                 self.pos.x = collide_wall[0].rect.right + self.rect.width / 2
        self.rect.centerx = self.pos.x
        self.pos.y += self.direction.y * self.speed * dt
        self.rect.centery = self.pos.y
        #print(self.rect.centerx/40,self.rect.centery/40,self.if_change_camera)
        if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
            collide_wall=pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
            if self.direction.y == 1 or self.direction.y == 0.7071:
                 self.pos.y = collide_wall[0].rect.top - self.rect.height / 2
            elif self.direction.y == -1 or self.direction.y == -0.7071:
                self.pos.y = collide_wall[0].rect.bottom + self.rect.height / 2
            self.rect.centery = self.pos.y
            self.pos = vec(self.rect.centerx, self.rect.centery)
    def cooldowns(self):
        now = pg.time.get_ticks()
        if not self.vulnerable:
            if now - self.hurt_time >= 1000:
                self.vulnerable = True
    def animate(self):
        now = pg.time.get_ticks()
        if now - self.last_update > 100:  # 每100毫秒更新一次帧
            self.last_update = now
            self.frame_index = (self.frame_index + 1) % len(self.current_frames)
            self.image = self.current_frames[self.frame_index]
        if not self.vulnerable:
            if cos(now)>0:
                self.image.set_alpha(0)
            else:
                self.image.set_alpha(255)
    def attack(self):
        now = pg.time.get_ticks()
        if now - self.last_update_attack >500:
            self.attack_sound.play()
            self.last_update_attack = now
            self.sword=Weapon(self)
            self.sword_timer=now
        if self.attack_direction.x == 1:
            self.current_frames = self.attack_frames_right
        elif self.attack_direction.x == -1:
            self.current_frames = self.attack_frames_left
        elif self.attack_direction.y == 1:
            self.current_frames = self.attack_frames_down
        elif self.attack_direction.y == -1:
            self.current_frames = self.attack_frames_up
    def check(self):
        if self.health<=0:
            pass
    def update(self, dt,camera):
        self.check() 
        self.draw(g_window,camera)
        self.input()
        self.move(dt)
        self.animate()
        self.cooldowns()
        if self.attacking:
            self.attacking = False
        if self.sword:
            self.sword.update(self)
            if pg.time.get_ticks()-self.sword_timer>200:
                self.sword.kill()
                self.sword=None
    def fix_to_middle(self, dx, dy):
        self.pos.x -= dx
        self.pos.y -= dy
    def draw(self,surface,camera):  # 定义显示实体的方法，该方法在场景需要描绘图像的时候调用
        rect =  self.rect.move(
            *(-i for i in camera))
        surface.blit(self.image, rect)  # 调用pygame的方法描绘图像   
        if self.sword:
            self.sword.update(self)
            sword_rect=self.sword.rect.move(  *(-i for i in camera))
            surface.blit(self.sword.image,sword_rect)
class spirit(pg.sprite.Sprite):
    def __init__(self,pos,index):
        pg.sprite.Sprite.__init__(self)
        self.load_images()
        self.index=index
        self.status = 'idle'
        self.frame_index = 0
        self.image = self.animations[self.status][0]
        self.rect = self.image.get_rect(topleft=pos)
        self.pos = vec(self.rect.centerx, self.rect.centery)
        self.health = monster_data['spirit']['health']
        self.exp = monster_data['spirit']['exp']
        self.attack_damage = monster_data['spirit']['damage']
        #self.attack_sound = pg.mixer.Sound(monster_data['spirit']['attack_sound'])
        self.speed = monster_data['spirit']['speed']
        self.resistance = monster_data['spirit']['resistance']
        self.attack_radius = monster_data['spirit']['attack_radius']
        self.notice_radius = monster_data['spirit']['notice_radius']
        self.vulnerable = True
        self.direction = vec(0, 0)
        self.can_attack = True
        self.attack_cooldown = 1000
        self.attack_time = 0

    def load_images(self):
       self.animations={
           'idle':[pg.image.load(GamePath.spirit_idle[i]) for i in range(4)],
           'attack':[pg.image.load(GamePath.spirit_attack[0])],
           'move':[pg.image.load(GamePath.spirit_move[i])for i in range(4)]
       }
    def get_player_distance_direction(self,player):
        enemy_vec = vec(self.rect.center)
        player_vec = vec(player.rect.center)
        distance = (player_vec - enemy_vec).magnitude()

        if distance > 0:
            direction = (player_vec - enemy_vec).normalize()
        else:
            direction = vec()

        return (distance,direction)
    def attack(self,player):
        if self.can_attack:
            if(player.vulnerable):
                player.health-=self.attack_damage
                player.vulnerable = False
                player.hurt_time=pygame.time.get_ticks()
    def get_status(self, player):
        distance = self.get_player_distance_direction(player)[0]
        if distance <= self.attack_radius and self.can_attack:
            if self.status != 'attack':
                self.frame_index = 0
            self.status = 'attack'
        elif distance <= self.notice_radius:
            self.status = 'move'
            self.direction = self.get_player_distance_direction(player)[1]
        else:
            self.status = 'idle'
    def action(self,player,dt):
        if self.status == 'attack':
            self.attack(player)
            self.direction = self.get_player_distance_direction(player)[1]
        elif self.status == 'move':
            self.direction = self.get_player_distance_direction(player)[1]
            self.move(dt)
        else:
            self.direction = vec(0, 0)
    def animate(self):
        animation = self.animations[self.status]
        
        self.frame_index += 0.1
        if self.frame_index >= len(animation):
            if self.status == 'attack':
                self.can_attack = False
            self.frame_index = 0

        self.image = animation[int(self.frame_index)]
        self.rect = self.image.get_rect(center = self.rect.center)
        if not self.vulnerable:
            now=pg.time.get_ticks()
            if cos(now)>0:
                self.image.set_alpha(0)
            else:
                self.image.set_alpha(255)
    def cooldowns(self):
        current_time = pg.time.get_ticks()
        if not self.can_attack:
            if current_time - self.attack_time >= self.attack_cooldown:
                self.can_attack = True
        if not self.vulnerable:
            if current_time-self.hit_time>=300:
                self.vulnerable=True
    def check_death(self):
        if self.health <= 0:
            self.kill()
    def hit_reaction(self):
        if not self.vulnerable:
            self.direction*=-3
            print(self.direction)
            return True
        else:
            return False
    def get_damage(self,player):
        if self.vulnerable:
            self.direction = self.get_player_distance_direction(player)[1]
            self.health-=player.attack_damage
            self.hit_time = pygame.time.get_ticks()
            self.vulnerable=False
    def draw(self,surface,camera):  # 定义显示实体的方法，该方法在场景需要描绘图像的时候调用
        rect =  self.rect.move(
            *(-i for i in camera))
        surface.blit(self.image, rect)  # 调用pygame的方法描绘图像   
    def move(self,dt):
        if(self.hit_reaction()):
            if self.direction!=(0,0):
                self.direction = self.direction.normalize()
            self.pos.x += self.direction.x * self.speed * dt*1.5
            self.rect.centerx = self.pos.x
            if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
                collide_wall = pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
                if self.direction.x ==1 or self.direction.x == 0.7071:
                    self.pos.x = collide_wall[0].rect.left - self.rect.width / 2
                elif self.direction.x == -1 or self.direction.x == -0.7071:
                    self.pos.x = collide_wall[0].rect.right + self.rect.width / 2
            self.rect.centerx = self.pos.x
            self.pos.y += self.direction.y * self.speed * dt*1.5
            self.rect.centery = self.pos.y
            if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
                collide_wall=pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
                if self.direction.y == 1 or self.direction.y == 0.7071:
                    self.pos.y = collide_wall[0].rect.top - self.rect.height / 2
                elif self.direction.y == -1 or self.direction.y == -0.7071:
                    self.pos.y = collide_wall[0].rect.bottom + self.rect.height / 2
            self.rect.centery = self.pos.y
            self.pos = vec(self.rect.centerx, self.rect.centery)
        else:
            if self.direction!=(0,0):
                self.direction = self.direction.normalize()
            self.pos.x += self.direction.x * self.speed * dt
            self.rect.centerx = self.pos.x
            if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
                collide_wall = pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
                if self.direction.x ==1 or self.direction.x == 0.7071:
                    self.pos.x = collide_wall[0].rect.left - self.rect.width / 2
                elif self.direction.x == -1 or self.direction.x == -0.7071:
                    self.pos.x = collide_wall[0].rect.right + self.rect.width / 2
            self.rect.centerx = self.pos.x
            self.pos.y += self.direction.y * self.speed * dt
            self.rect.centery = self.pos.y
            if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
                collide_wall=pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
                if self.direction.y == 1 or self.direction.y == 0.7071:
                    self.pos.y = collide_wall[0].rect.top - self.rect.height / 2
                elif self.direction.y == -1 or self.direction.y == -0.7071:
                    self.pos.y = collide_wall[0].rect.bottom + self.rect.height / 2
            self.rect.centery = self.pos.y
            self.pos = vec(self.rect.centerx, self.rect.centery)
    def update(self,player,dt,camera):
        self.draw(g_window,camera)
        self.hit_reaction()
        self.move(dt=dt)
        self.animate()
        self.cooldowns()
        self.check_death()
        self.get_status(player)
        self.action(player,dt=dt)
def gen_monster():
     index=0
     monsters = []
     
     for _ in range(20):
            for i in range(len(MapSettings.matrix)):
                for j in range(len(MapSettings.matrix[i])):
                    if MapSettings.matrix[i][j] == 2:
                        if MapSettings.Room_1['x1']<j<MapSettings.Room_1['x2'] and MapSettings.Room_1['y1']<i<MapSettings.Room_1['y2']:
                            flag = random.randint(0, 750)
                            if flag ==8 and len(monsters)<=6:
                                monsters.append(spirit((j*40,i*40),index))
                                index+=1
                                MapSettings.matrix[i][j] = 3
                        if MapSettings.Room_2['x1']<j<MapSettings.Room_2['x2'] and MapSettings.Room_2['y1']<i<MapSettings.Room_2['y2']:
                            flag = random.randint(0, 750)
                            if flag ==8 and len(monsters)<=12:
                                monsters.append(spirit((j*40,i*40),index))
                                index+=1
                                MapSettings.matrix[i][j] = 3
     return monsters                              
class bullet(pg.sprite.Sprite):
    def __init__(self,direction,pos):
        pg.sprite.Sprite.__init__(self)
        self.direction=direction
        self.load_images()
        self.status='moving'
        self.speed=100
        bullet.attack_damage=5
        self.image = self.animations[self.status][0]
        self.frame_index=0
        self.rect = self.image.get_rect(topleft=pos)
    def load_images(self):
        self.animations={"moving":[pg.image.load(GamePath.bullets_projectile[i]) for i in range(3)],
                         "destroyed":[pg.image.load(GamePath.bullets_destroyed[i]) for i in range(3)]
        }
    def take_the_damage(self,player):
        if(pygame.sprite.collide_rect(player,self)):
            self.kill()
            if(player.vulnerable):
                player.health-=self.attack_damage
                player.vulnerable = False
                player.hurt_time=pygame.time.get_ticks()
    def action(self,dt):
        if self.status=="moving":
            self.move(dt)
    def move(self,dt):
        if self.direction!=(0,0):
            self.direction = self.direction.normalize()
            self.rect.centerx += self.direction.x * self.speed * dt
            self.rect.centery += self.direction.y * self.speed * dt
        if(pygame.sprite.spritecollideany(self, wall_sprite,collided = None)):
            self.kill()
    def animate(self):
        animation = self.animations[self.status]
        
        self.frame_index += 0.3
        if self.frame_index >= len(animation):
            self.frame_index = 0
        self.image = animation[int(self.frame_index)]
        self.rect = self.image.get_rect(center = self.rect.center)
    def draw(self,surface,camera):  # 定义显示实体的方法，该方法在场景需要描绘图像的时候调用
        rect =  self.rect.move(
            *(-i for i in camera))
        surface.blit(self.image, rect)  # 调用pygame的方法描绘图像   
    def update(self,player,dt,camera):
        self.draw(g_window,camera)
        self.move(dt=dt)
        self.animate()
        self.take_the_damage(player=player)
        self.action(dt)

    

class eye_beast(pg.sprite.Sprite):
    def __init__(self,pos):
        pg.sprite.Sprite.__init__(self)
        self.load_images()
        self.status = 'idle'
        self.fireball_sound=pygame.mixer.Sound(r".\assets\audio\fireball.mp3")
        self.frame_index=0
        self.fireball_sound.set_volume(5)
        self.image = self.animations[self.status][0]
        self.image=pygame.transform.scale(self.image,(400,400))
        self.rect = self.image.get_rect(topleft=pos)
        self.pos = vec(self.rect.centerx, self.rect.centery)
        self.health = monster_data['eye_beast']['health']
        self.exp = monster_data['eye_beast']['exp']
        self.attack_damage = monster_data['eye_beast']['damage']
        #self.attack_sound = pg.mixer.Sound(monster_data['spirit']['attack_sound'])
        self.speed = monster_data['eye_beast']['speed']
        self.resistance = monster_data['eye_beast']['resistance']
        self.notice_radius = monster_data['eye_beast']['notice_radius']
        self.vulnerable = True
        self.direction = vec(0, 0)
        self.can_attack = True
        self.attack_radius= monster_data['eye_beast']['attack_radius']
        self.attack_cooldown = 500
        self.attack_time = 0
        self.bullet_sprite=pg.sprite.Group()

    def load_images(self):
       self.animations={
           'idle':[pg.image.load(GamePath.eye_beast_idle[i]) for i in range(20)],
           'attack':[pg.image.load(GamePath.eye_beast_attack[i]) for i in range(13)],
           'move':[pg.image.load(GamePath.eye_beast_move[i])for i in range(13)]
       }
    def get_player_distance_direction(self,player):
        enemy_vec = vec(self.rect.center)
        player_vec = vec(player.rect.center)
        distance = (player_vec - enemy_vec).magnitude()

        if distance > 0:
            direction = (player_vec - enemy_vec).normalize()
        else:
            direction = vec()

        return (distance,direction)
    def attack(self,player):
        if self.can_attack:
            bullet1=bullet(self.get_player_distance_direction(player)[1],pos=(self.rect.left+105,(self.rect.top+self.rect.bottom)/2))
            self.bullet_sprite.add(bullet1)
            self.fireball_sound.play()
            self.can_attack=False
            self.attack_time =pygame.time.get_ticks()
    def get_status(self, player):
        distance = self.get_player_distance_direction(player)[0]
        if distance <= self.attack_radius and self.can_attack:
            if self.status != 'attack':
                self.frame_index = 0
            self.status = 'attack'
        elif distance <= self.notice_radius:
            self.status = 'move'
            self.direction = self.get_player_distance_direction(player)[1]
        else:
            self.status = 'idle'
    def action(self,player,dt):
        if self.status == 'attack':
            self.attack(player)
            self.direction = self.get_player_distance_direction(player)[1]
        elif self.status == 'move':
            self.direction = self.get_player_distance_direction(player)[1]
            self.move(dt)
        else:
            self.direction = vec(0, 0)
    def animate(self):
        animation = self.animations[self.status]
        
        self.frame_index += 0.1
        if self.frame_index >= len(animation):
            if self.status == 'attack':
                self.can_attack = False
            self.frame_index = 0

        self.image = animation[int(self.frame_index)]
        self.image=pygame.transform.scale(self.image,(400,400))
        self.rect = self.image.get_rect(center = self.rect.center)
        if not self.vulnerable:
            now=pg.time.get_ticks()
            if cos(now)>0:
                self.image.set_alpha(0)
            else:
                self.image.set_alpha(255)
    def cooldowns(self):
        current_time = pg.time.get_ticks()
        if not self.can_attack:
            if current_time - self.attack_time >= self.attack_cooldown:
                self.can_attack = True
        if not self.vulnerable:
            if current_time-self.hit_time>=300:
                self.vulnerable=True
    def check_death(self):
        if self.health <= 0:
            self.kill()
    def hit_reaction(self):
        if not self.vulnerable:
            self.direction*=-9
            print(self.direction)
            return True
        else:
            return False
    def get_damage(self,player):
        if self.vulnerable:
            print("i am hurt")
            print(self.health)
            self.direction = self.get_player_distance_direction(player)[1]
            self.health-=player.attack_damage
            self.hit_time = pygame.time.get_ticks()
            self.vulnerable=False
    def draw(self,surface,camera):  # 定义显示实体的方法，该方法在场景需要描绘图像的时候调用
        rect =  self.rect.move(
            *(-i for i in camera))
        surface.blit(self.image, rect)  # 调用pygame的方法描绘图像   
    def move(self,dt):
        if(self.hit_reaction()):
            if self.direction!=(0,0):
                self.direction = self.direction.normalize()
            self.pos.y += self.direction.y * self.speed * dt*1.5
            self.rect.centery = self.pos.y
            if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
                collide_wall=pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
                if self.direction.y == 1 or self.direction.y == 0.7071:
                    self.pos.y = collide_wall[0].rect.top - self.rect.height / 2
                elif self.direction.y == -1 or self.direction.y == -0.7071:
                    self.pos.y = collide_wall[0].rect.bottom + self.rect.height / 2
            self.rect.centery = self.pos.y
            self.pos = vec(self.rect.centerx, self.rect.centery)
        else:
            if self.direction!=(0,0):
                self.direction = self.direction.normalize()
            self.pos.y += self.direction.y * self.speed * dt
            self.rect.centery = self.pos.y
            if(pygame.sprite.spritecollideany(self, wall_sprite, collided = None)):
                collide_wall=pygame.sprite.spritecollide(self, wall_sprite, False, collided = None)
                if self.direction.y == 1 or self.direction.y == 0.7071:
                    self.pos.y = collide_wall[0].rect.top - self.rect.height / 2
                elif self.direction.y == -1 or self.direction.y == -0.7071:
                    self.pos.y = collide_wall[0].rect.bottom + self.rect.height / 2
            self.rect.centery = self.pos.y
            self.pos = vec(self.rect.centerx, self.rect.centery)
    def update(self,player,dt,camera):
        self.draw(g_window,camera)
        self.hit_reaction()
        self.move(dt=dt)
        self.animate()
        self.cooldowns()
        self.check_death()
        self.get_status(player)
        self.action(player,dt=dt)
        self.bullet_sprite.update(player,dt,camera)